﻿using System;
using System.Windows.Forms;
using Business;
using Business.Entity.Contabil.Pessoa.SubClass.Usuario.ClassesRelacionadas;
using Business.Entity.Sped;
using Business.Forms;
using Business.Seguranca;
using Business.Validation;
using DevExpress.XtraEditors;

namespace Bonesoft.Forms.Sped
{
    public partial class FormPlanoContaReferencial : FormDefault, IForm
    {
        public FormPlanoContaReferencial()
        {
            InitializeComponent();

            planoContaBindingSource.DataSource = PlanoContaReferencialRepository.GetList();
        }

        public object Novo()
        {
            var planoConta = new PlanoContaReferencial();
            CurrentObject = planoConta;
            ShowDialog();
            if (planoConta.Id == 0)
            {
                return null;
            }
            return planoConta;
        }

        public void Alterar(object entity)
        {
            CurrentObject = entity;
            ShowDialog();
        }

        public object Save()
        {
            if (!Validate().HasError)
            {
                PlanoContaReferencialRepository.Save((PlanoContaReferencial)CurrentObject);
            }
            return CurrentObject;
        }

        public void Update(object entity)
        {
            Alterar(entity);
        }

        public bool Delete(object entity)
        {
            CurrentObject = entity;
            PlanoContaReferencialRepository.Delete((PlanoContaReferencial) CurrentObject);
            return true;
        }


        public object CurrentObject
        {
            get
            {
                planoContaBindingSource.EndEdit();
                return planoContaBindingSource.DataSource;
            }
            set
            {
                planoContaBindingSource.DataSource = value;
            }
        }

        public PermissaoForm Permissao
        {
            get
            {
                return SegurancaUtils.GetPermissaoForm(Name);
            } 
            
        }

        public EntityValidationResult Validate()
        {
            return DataValidation.ValidateEntity(CurrentObject);
        }

        public void Reload()
        {
            throw new NotImplementedException();
        }

        public void Cancel()
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        public Object New()
        {
            return Novo();
        }

        public XtraForm Form
        {
            get
            {
                return this;
            }
        }

        public FormState FormState { get; set; }

        private void planoContaBindingSource_PositionChanged(object sender, EventArgs e)
        {

        }

        private void txtCodigo_KeyDown(object sender, KeyEventArgs e)
        {



        }

        private void txtCodigo_TextChanged(object sender, EventArgs e)
        {
            if (txtCodigo.Text.Equals(""))
            {
                return;
            }
            var chars = txtCodigo.Text.ToCharArray();
            var lastChar = chars[chars.Length - 1];
            if (lastChar == '.')
            {
                var codico = txtCodigo.Text.Substring(0, txtCodigo.Text.Length - 2);
                var item = planoContaBindingSource.Find("Codigo", txtCodigo.Text);

            }
        }
    }
}
